<template>
  <div id="login">
    <div class="mesBox">
      <div class="mesBoxson">
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          status-icon>
          <el-form-item label="账号" prop="userNum">
            <el-input v-model.trim="ruleForm.userNum" />
          </el-form-item>
          <el-form-item label="密码" prop="userPassword">
            <el-input v-model.trim="ruleForm.userPassword" />
          </el-form-item>
          <el-form-item>
            <div style="width: 100%; text-align: center;">
              <el-button type="primary" @click="submitForm()">
                登录
              </el-button>
              <el-button @click="resetForm()">注册</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

</template>
<script setup>
import { reactive, ref, inject } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition)
} else {
  document.getElementById('demo').innerHTML =
  'Geolocation is not supported by this browser.'
}

function showPosition (position) {
  document.getElementById('demo').innerHTML =
  'Latitude: ' + position.coords.latitude +
  'Longitude: ' + position.coords.longitude
}

// vuex state 数据导出
const user = useStore()
const getIdArr = user.state.roleManage

// 使用element messageBox
const messBox = inject('$overAll')

// 路由API
const router = useRouter()

// 登录、注册 data
const ruleFormRef = ref(null)
const ruleForm = reactive({
  userPassword: '',
  userNum: ''
})
const rules = reactive({
  userNum: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  userPassword: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

// function 查看输入的用户名字是否为vuex字典内的
const getUser = (val) => {
  for (const item of getIdArr) {
    for (const jusIt of item.userList) {
      if (val === jusIt) {
        sessionStorage.setItem('userTity', item.user)
        return true
      }
    }
  }
}
// 登录
const submitForm = async () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      if (getUser(ruleForm.userNum)) {
        router.push({
          name: 'index'
        })
      } else {
        messBox.$ElMessageBox.confirm(
          '账号或密码有误',
          '错误提示',
          {
            showCancelButton: true,
            cancelButtonText: '取消',
            showConfirmButton: true,
            confirmButtonText: '确定',
            showClose: true,
            distinguishCancelAndClose: true,
            closeOnClickModal: false,
            type: 'warning',
            draggable: true
          }
        )
          .then(() => {
            messBox.$ElMessageBox.close()
          })
          .catch(() => {
            messBox.$ElMessageBox.close()
          })
      }
    }
  })
}
// 切换注册
const resetForm = async (formEl) => {

}
</script>
<style lang="scss" scoped>
#login {
  width: 100%;
  height: 100vh;
  display: flex;
  background: linear-gradient(135deg, #ff00cc, #ffcc00, #00ffcc, #ff0066);

  align-items: center;
  justify-content: center;
  .mesBox {
    width: 300px;
    height: 200px;
    border: 1px solid #000;
    display: flex;
    align-items: center;
    justify-content: center;
    .mesBoxson {
      width: 200px;
    }
  }
}
</style>
